package com.example.demo.modules.oss.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.util.R;
import com.example.demo.modules.oss.entity.ImageList;
import com.example.demo.modules.oss.service.ImageListService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Optional;

@RestController
@RequestMapping("/api/imageList")
public class ImageListController {

    @Autowired
    private ImageListService imageListService;


    @GetMapping("/one")
    public R one(@RequestParam(value = "id", required = true) String id) {
        if (null == id || "".equals(id)) {
            return R.error().put("msg", "缺少参数id");
        }

        return R.ok().put("result", imageListService.getById(id));
    }

    @GetMapping("/listAll")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字", dataType = "string", paramType = "query", example = ""),
    })
    public R listAll(
            @RequestParam HashMap<String, String> params
    ) {
        return R.ok().put("result", imageListService.list(null));
    }

    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageNum", dataType = "String", required = true),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataType = "string", paramType = "query", example = ""),
    })
    public R page(
            Integer pageSize, Integer pageNum, @RequestParam HashMap<String, String> params) {
        if (null == pageSize || "".equals(pageSize)) {
            pageSize = 10;
        }
        if (null == pageNum || "".equals(pageNum)) {
            pageNum = 0;
        }
        QueryWrapper<ImageList> queryWrapper = new QueryWrapper<>();
        Optional.ofNullable(params.get("keyword")).ifPresent((o)->{
            queryWrapper.like("name", params.get("keyword"));
        });
        return R.ok().put("result", imageListService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @PostMapping("/add")
    public R add(@RequestBody ImageList imageList) {
        return R.ok().put("result", imageListService.save(imageList));
    }

    @PostMapping("/update")
    public R update(@RequestBody ImageList imageList, HashMap<String, Object> updateOptions) {
        QueryWrapper<ImageList> queryWrapper = new QueryWrapper<>();
        if (null != imageList.getId() && !("".equals(imageList.getId()))) {
            queryWrapper.eq("id", imageList.getId());
        } else {
            return R.error("id不能为空");
        }
        return R.ok().put("result", imageListService.update(imageList, queryWrapper));
    }

    @GetMapping("/delete")
    public R deleteOne(@RequestParam(value = "id", required = true) String id) {
        return R.ok().put("result", imageListService.removeById(id));
    }
}
